package com.sskj.login.ui.activity;

import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.LazyLoaders;
import com.gyf.barlibrary.ImmersionBar;
import com.hey.lib.HeySpinner;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.enums.AreaCodeEnum;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.box.verifier.ToastNextInputs;
import com.sskj.lib.util.CaptchaUtil;
import com.sskj.lib.util.EditUtil;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.lib.util.SendCodeUtil;
import com.sskj.login.R;
import com.sskj.login.R2;
import com.sskj.login.presenter.RegisterActivityPresenter;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * 注册
 */
@Route(path = RConfig.LOGIN_REGISTER)
public class RegisterActivity extends BaseActivity<RegisterActivityPresenter> {

    @BindView(R2.id.etAccountNumber)
    EditText etAccountNumber;

    @BindView(R2.id.etCheckCode)
    EditText etCheckCode;
    @BindView(R2.id.tvCheckCode)
    TextView tvCheckCode;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.ivPwdToggle)
    ImageView ivPwdToggle;
    @BindView(R2.id.etPwdAgain)
    EditText etPwdAgain;
    @BindView(R2.id.ivPwdToggleAgain)
    ImageView ivPwdToggleAgain;
    @BindView(R2.id.etInviteCode)
    EditText etInviteCode;
    @BindView(R2.id.checkbox)
    CheckBox checkbox;
    @BindView(R2.id.tvAgreement)
    TextView tvAgreement;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    @BindView(R2.id.tvLogin)
    TextView tvLogin;

    private Disposable timeDis;
    private String phoneNumber, messageCode, loginPwd, confirmPwd, visitCode;
    ToastNextInputs inputs;
    private LazyLoaders lazyLoaders;
    private ToastNextInputs inputsCode;
    private AreaCodeEnum areaCodeEnumChoose;
    @BindView(R2.id.heySpinner)
    HeySpinner heySpinner;
    @Override
    public int getLayoutId() {
        ImmersionBar.with(this).statusBarColor(R.color.lib_dominant).statusBarDarkFont(false).init();
        return R.layout.login_activity_register;
    }

    @Override
    public RegisterActivityPresenter getPresenter() {
        return new RegisterActivityPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        inputs = new ToastNextInputs();
        inputsCode = new ToastNextInputs();
        lazyLoaders = new LazyLoaders(this);
        inputs.clear();
        inputsCode.clear();
        inputsCode.add(etAccountNumber, SchemeUtil.notEmpty(etAccountNumber), SchemeUtil.phone())
        ;
        inputs.add(etAccountNumber, SchemeUtil.notEmpty(etAccountNumber), SchemeUtil.phone())

                .add(etCheckCode, SchemeUtil.notEmpty(etCheckCode), SchemeUtil.smsCode())
                .add(etPwd, SchemeUtil.notEmpty(etPwd), SchemeUtil.pwd())
                .add(etPwdAgain, SchemeUtil.notEmpty(etPwdAgain), SchemeUtil.equalsPwd(lazyLoaders.fromEditText(etPwd)))
                .add(etInviteCode, SchemeUtil.notEmpty(etInviteCode))
                .add(checkbox, SchemeUtil.checkbox(checkbox))
        ;
        heySpinner.setVisibility(Constans.isAreaCode ? View.VISIBLE : View.GONE);

        SendCodeUtil.areaCode(this,heySpinner,areaCodeEnum -> {
            areaCodeEnumChoose = areaCodeEnum;
        });

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ClickUtil.click(tvCheckCode, () -> {
            if (inputsCode.test()) {
                registerCheck();
            }
        });
        ClickUtil.click(200, tvAgreement, () -> {
            ARouter.getInstance().build(RConfig.HANG_GUIDE_WEB)
                    .withBoolean(Constans.IS_REGISTER_AGREEMENT, true)
                    .navigation();
        });

        ClickUtil.click(btSubmit, () -> {
            if (inputs.test()) {
                phoneNumber = getText(etAccountNumber);

                messageCode = getText(etCheckCode);
                loginPwd = getText(etPwd);
                confirmPwd = getText(etPwdAgain);
                visitCode = getText(etInviteCode);

                CaptchaUtil.check(this, (result, validate, msg) -> btSubmit.postDelayed(() ->
                                mPresenter.register(phoneNumber, loginPwd, visitCode, messageCode, areaCodeEnumChoose.getAreaCode() ),
                        300));
            }
        });

        EditUtil.togglePs(etPwd, ivPwdToggle);
        EditUtil.togglePs(etPwdAgain, ivPwdToggleAgain);
        ClickUtil.click(200, tvLogin, this::finish);
    }

    public void sendCheckCodeSuccess() {
        DisposUtil.close(timeDis);
        timeDis = SendCodeUtil.timeCount(this, tvCheckCode);
    }

    public void sendCheckCodeFail() {
        tvCheckCode.setEnabled(true);
    }

    public void registerSuccess() {
        LiveDataBus.get().with(RxBusCode.REGISTER_ACCOUNT, String.class)
                .postValue(etAccountNumber.getText().toString());
        finish();
    }

    public void registerCheck() {
        CaptchaUtil.check(this, (result, validate, msg) -> {
            tvCheckCode.setEnabled(false);
            mPresenter.sendCode(validate, etAccountNumber.getText().toString(),areaCodeEnumChoose.getAreaCode());
        });
    }
}
